628D - Magic Numbers - CodeForces Solution


dp *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>

using namespace std;

template<typename typC> istream &operator>>(istream &cin,vector<typC> &a) { for (int i=0;i<a.size();i++) cin>>a[i]; return cin; }
template<typename typC> ostream &operator<<(ostream &cout,const vector<typC> &a) { int n=a.size(); if (!n) return cout; cout<<a[0]; for (int i=1; i<n; i++) cout<<' '<<a[i]; return cout; }
typedef long long ll;

#define debug if (0) cout

int m, d;
ll dp[2002][2][2][2002]; // pos, odd/even, /smaller, m remainder
string value;
constexpr ll MOD = 1000000007;

ll calculate(int pos, int isEven, int isSmaller, int r) {
  if (pos == value.size()) {
    return r == 0 ? 1 : 0;
  }
  
  if (dp[pos][isEven][isSmaller][r] >= 0) {
    return dp[pos][isEven][isSmaller][r];
  }

  int upLimit = isSmaller ? 9 : (value[pos] - '0');
  
  dp[pos][isEven][isSmaller][r] = 0;
  if (isEven) {
    if (d > upLimit) {
      // could not chose d;
      return dp[pos][isEven][isSmaller][r] = 0;
    }
    dp[pos][isEven][isSmaller][r] +=
        calculate(pos + 1, 1 - isEven, isSmaller || d < (value[pos] - '0'), (r * 10 + d) % m);
    dp[pos][isEven][isSmaller][r] %= MOD;
  } else {
    for (int i = 0; i <= upLimit; i++) {
      if (i == d) continue;
      dp[pos][isEven][isSmaller][r] +=
          calculate(pos + 1, 1 - isEven, isSmaller || i < (value[pos] - '0'), (r * 10 + i) % m);
      dp[pos][isEven][isSmaller][r] %= MOD;
    }
  }

  return dp[pos][isEven][isSmaller][r];
}

ll solve(string v) {
  memset(dp, -1, sizeof(dp));
  value = v;

  ll ans = calculate(0, 0, 0, 0);
  debug << v << " : " << ans << endl;
  return ans;
}

int isOK(string a) {
  // check d-magic
  for (int i = 0; i < a.size(); i++) {
    if (i % 2 == 0) {
      // odd pos
      if (a[i] - '0' == d) {
        return 0; // false
      }
    } else {
      // even pos
      if (a[i] - '0' != d) {
        return 0; // false
      }
    }
  }
  
  // divisible 
  int v = 0;
  for (int i = 0; i < a.size(); i++) {
    v = (v * 10 + (a[i] - '0')) % m;
  }
  if (v == 0) return 1; else return 0;
}
void solve() {
  cin >> m >> d;
  string a, b;
  cin >> a >> b;
  
  ll ans = (solve(b) + MOD - solve(a) + isOK(a)) % MOD;
  cout << ans << endl;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  
  int cases = 1;
  // cin >> cases;
  for (int i = 0; i < cases; i++) {
    debug << "cases : " << i + 1 << endl;
    solve();
    debug << endl;
  }
  
  return 0;
}


Comments

Submit
0 Comments
More Questions

1650A - Deletions of Two Adjacent Letters
1512A - Spy Detected
282A - Bit++
69A - Young Physicist
1651A - Playoff
734A - Anton and Danik
1300B - Assigning to Classes
1647A - Madoka and Math Dad
710A - King Moves
1131A - Sea Battle
118A - String Task
236A - Boy or Girl
271A - Beautiful Year
520B - Two Buttons
231A - Team
479C - Exams
1030A - In Search of an Easy Problem
158A - Next Round
71A - Way Too Long Words
160A - Twins
1A - Theatre Square
1614B - Divan and a New Project
791A - Bear and Big Brother
1452A - Robot Program
344A - Magnets
96A - Football
702B - Powers of Two
1036A - Function Height
443A - Anton and Letters
1478B - Nezzar and Lucky Number